/* Author:

*/




 $(document).ready(function() {

	 
	 $(window).resize(function(){
			$("#map_canvas4_1").css('height',($(window).height() - $("#header4_1").height() - 35));
			$("#map_canvas4").css('height',($(window).height() - $("#header4").height() - 35));
			
	 });		 
	 
	 
	$("#page4").live( "pagebeforecreate", function(){

			$.get('dati/musei.json', function(xml){
				app.musei = xml;
				
				var top = xml.meta.view.columns[11].cachedContents.top;

				//var json = $.xml2json(xml);	
				$( "#tmpMappaMusei" ).tmpl( top ).appendTo( "#lvMappaMusei" );
				$('#lvMappaMusei').listview('refresh');
				

			});

	});

	$("#page4_1").live( "pagebeforeshow", function(item,item2){
		
		
		
			$("#h14_1").html(app.MappaMuseiSelectedItem);
//
//				var items = app.musei.data;
//
//				items = jQuery.grep(items, function(n, i){
//					  return (n[11] == app.MappaMuseiSelectedItem);
//				});
//				
//				initialize4_1(items);

				
	});


	 
	 $("#page4_1").live("pageshow",function(page){
		
		 var contentH =  $(window).height() - $("#header4_1").height() - 35;

		 
		 $("#bt4_1").click(function(){
	
			 $("#h14_1").html('Musei '+app.musei.data.length); 
			 
			 initialize4_1(app.musei.data);
		 });	
		 
		 

			
			
			
			
			 $("#map_canvas4_1").css('height',contentH);
			 
			 
				$("#h14_1").html(app.MappaMuseiSelectedItem);

				var items = app.musei.data;

				items = jQuery.grep(items, function(n, i){
					  return (n[11] == app.MappaMuseiSelectedItem);
				});
				
				$("#h14_1").html(app.MappaMuseiSelectedItem+' '+items.length);
				
				
				initialize4_1(items);	
				
				
				
				 $("#map_canvas4_1").css('height',contentH);
	 });
	 
	
  });
	
	
	
	
	function initialize4_1(items) {
		
		
		
		 app.myLatLng = (app.myLatLng == undefined) ? new google.maps.LatLng(45.464683238625966, 9.18879747390747) : app.myLatLng;
		  		  
		  
		  
		  var myOptions = {
		    zoom: 9,
		    center: app.myLatLng,
		    disableDefaultUI: true,
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		  };

		  var map = new google.maps.Map(document.getElementById("map_canvas4_1"), myOptions);	
		  

		  var placeMarker = new google.maps.Marker({position: app.myLatLng, map: map});

			$.each( items, function(i, marker) {
	
				var lat = marker[65][1];
				var lng = marker[65][2];
	
				var contentString = '<div style="float:left;font-size:0.9em;line-height:1.2em;color:#888;font-style:italic; cursor:auto; overflow:auto">'+
				//'<img src="img/map/bed_breakfast1-2.png">'+
		  		'<p><strong>'+marker[10]+'</strong><br>'+
				'indirizzo: '+marker[17]+', '+marker[13]+'<br />'+
			    'telefono: '+marker[18]+'<br />'+
			    //'numero posti letto: '+marker.numero_posti_letto+'<br />'+
			    '<a href="http://'+marker[21]+'" target="_blank">'+marker[21]+'</a><br />'+
			    '<br /><a href="http://maps.google.com/maps?q='+lat+','+lng+'" target="_blank">View in Google Maps</a>'+
		  		'<br /></p>'+
		  		'</div>';						
				
				var infowindow = new google.maps.InfoWindow({
					content:  contentString
				});
				
				
				
				  var marker = new google.maps.Marker({
				      position: new google.maps.LatLng(lat, lng),
				      map: map,
				      animation: google.maps.Animation.DROP,
				      icon: new google.maps.MarkerImage('img/map/temple-2.png')
				  });
				  
				  google.maps.event.addListener(marker, 'click', function() { infowindow.open(map,marker); });
	
			});					
				
				
			
			
			
		/*
		
		$.get('dati/ostelli.xml', function(xml){
			var json = $.xml2json(xml);

			
			$.each( json.row.row, function(i, marker) {

				var contentString = '<div id="tooltip">'+
				'<img src="img/map/bed_breakfast1-2.png">'+
		  		'<p><strong>'+marker.denominazione+'</strong><br>'+
				'indirizzo: '+marker.indirizzo+', '+marker.comune_provincia+'<br />'+
			    'telefono: '+marker.telefono+'<br />'+
			    'numero posti letto: '+marker.numero_posti_letto+'<br />'+
			    'sito internet: <a href="http://'+marker.sito_internet+'" target="_blank">'+marker.sito_internet+'</a><br />'+
		  		'<br /></p>'+
		  		'<br /></div>';				
				
				
				var infowindow = new google.maps.InfoWindow({
					content:  contentString
				});
				
				
				
				
				  var marker = new google.maps.Marker({
				      position: new google.maps.LatLng(marker.lat, marker.lng),
				      map: map,
				      title: marker.denominazione,
				      icon: new google.maps.MarkerImage('img/map/bed_breakfast1-2.png')
				  });
				  
				  google.maps.event.addListener(marker, 'click', function() {
					  
					  
					  
					  infowindow.open(map,marker);
					  
					  
					});

			});			
			
			
		});		
		
		*/
		
		
		
//		$.getJSON( 'dati/rows.json', function(data) { 
//			
//			
//
//		});			
		

		}

	
	
	
	
	